package com.wyh.lotterysystem.common.pojo;

import com.wyh.lotterysystem.common.errorcode.ErrorCode;
import com.wyh.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一返回格式
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    //状态码
    private Integer code;
    //状态描述
    private String msg;
    //数据
    private T data;

    /**
     * 成功
     * 直接调用返回CommonResult对象
     */
    public static <T> CommonResult<T> success(T data){
        CommonResult<T> result = new CommonResult<>();
        result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        result.setData(data);
        return result;
    }

    /**
     * 失败
     * 直接调用返回CommonResult对象
     */
    public static <T> CommonResult<T> fail(Integer code, String msg){
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.msg = msg;
        return result;
    }

    /**
     * 方法重载，支持传ErrorCode对象
     */
    public static <T> CommonResult<T> fail(ErrorCode errorCode){
        CommonResult<T> result = new CommonResult<>();
        result.code = errorCode.getCode();
        result.msg = errorCode.getMsg();
        return result;
    }

}
